public class Solution {
    public void duplicateZeros(int[] arr) {
        int n = arr.length;
        int count = 0;
        int i = -1;

        while(count < n){
            i++;
            if(arr[i] == 0) count++;
            count++;
        }
        int j = n - 1;

        //如果最后一位是0， 可能会出现数组越界的情况
        if(count > n && arr[i] == 0){
            arr[j] = 0;
            j--;
            i--;
        }

        while(j >= 0){
            arr[j] = arr[i];
            if(arr[j] == 0){
                arr[j - 1] = 0;
                j--;
            }
            j--;
            i--;
        }

    }
}
